<?php

class Controller_Site extends Controller_Abstract
{
	public function actionShow()
	{
		//TODO: unknown
		$cremodel = new Creative();
		$sitemodel = new Site();
		$sites = $sitemodel->getAllSites();
		$this->_view['sites'] = $sites;
		if (isset($_SESSION['infos']))
		{
			$infos = $_SESSION['infos'];
			$this->_view['infos'] = $infos;
			unset($_SESSION['infos']);
		}
	}
	public function actionAdd()
	{
		$this->_view['monetizations'] = DataUtil::getOptionsForMonetization();
		$this->_view['templates'] = Util::getTemplates(Q::ini('appini/template_root'));
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$name = $this->_context->name;
			$domain = strtolower($this->_context->domain);
			$keywords = $this->_context->keywords;
			$description = $this->_context->description;
			$game = $this->_context->game;
			$jackpot = $this->_context->jackpot;
			$jackpotPercent = $this->_context->jackpot_percent;
			$template = $this->_context->template;
			$monetization1 = $this->_context->monetization1;
			$monetization2 = $this->_context->monetization2;
			$monetization3 = $this->_context->monetization3;
			$monetization4 = $this->_context->monetization4;
			$topAd = $this->_context->ad_top;
			$rightAd = $this->_context->ad_right;
			$bottomAd = $this->_context->ad_bottom;
			$p5 = $this->_context->query('p5', 0);
			$p10 = $this->_context->query('p10', 0);
			$p15 = $this->_context->query('p15', 0);
			$p20 = $this->_context->query('p20', 0);
			$p25 = $this->_context->query('p25', 0);
			$p30 = $this->_context->query('p30', 0);
			$p35 = $this->_context->query('p35', 0);
			$p40 = $this->_context->query('p40', 0);
			$p45 = $this->_context->query('p45', 0);
			$p50 = $this->_context->query('p50', 0);
			$locale = $this->_context->locale;
			if (empty($domain))
			{
				$errors[] = Util::getMessage(2001);
			}
			if (count($errors) == 0)
			{
				$sitemodel = new Site();
				$sitemodel->add($name, $domain, $game, $jackpot, $jackpotPercent, $template, $monetization1, $monetization2, $monetization3, $monetization4, $topAd, $rightAd, $bottomAd, $p5, $p10, $p15
								, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description);
				$infos[] = Util::getMessage(2002);
				$this->_view['infos'] = $infos;
			}
			else
			{
				$this->_view['errors'] = $errors;
				$this->_view['name'] = $name;
				$this->_view['domain'] = $domain;
				$this->_view['game'] = $game;
				$this->_view['jackpot'] = $jackpot;
				$this->_view['jackpot_percent'] = $jackpotPercent;
				$this->_view['template'] = $template;
				$this->_view['monetization1'] = $monetization1;
				$this->_view['monetization2'] = $monetization2;
				$this->_view['monetization3'] = $monetization3;
				$this->_view['monetization4'] = $monetization4;
				$this->_view['ad_top'] = $topAd;
				$this->_view['ad_right'] =$rightAd;
				$this->_view['ad_bottom'] = $bottomAd;
				$this->_view['p5'] = $p5;
				$this->_view['p10'] = $p10;
				$this->_view['p15'] = $p15;
				$this->_view['p20'] = $p20;
				$this->_view['p25'] = $p25;
				$this->_view['p30'] = $p30;
				$this->_view['p35'] = $p35;
				$this->_view['p40'] = $p40;
				$this->_view['p45'] = $p45;
				$this->_view['p50'] = $p50;
				$this->_view['locale'] = $locale;
				$this->_view['keywords'] = $keywords;
				$this->_view['description'] = $description;
			}
		}
	}
	public function actionEdit()
	{
		$this->_view['monetizations'] = DataUtil::getOptionsForMonetization();
		$this->_view['templates'] = Util::getTemplates(Q::ini('appini/template_root'));
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$id = $this->_context->id;
			$name = $this->_context->name;
			$domain = strtolower($this->_context->domain);
			$keywords = $this->_context->keywords;
			$description = $this->_context->description;
			$game = $this->_context->game;
			$jackpot = $this->_context->jackpot;
			$jackpotPercent = $this->_context->jackpot_percent;
			$template = $this->_context->template;
			$monetization1 = $this->_context->monetization1;
			$monetization2 = $this->_context->monetization2;
			$monetization3 = $this->_context->monetization3;
			$monetization4 = $this->_context->monetization4;
			$topAd = $this->_context->ad_top;
			$rightAd = $this->_context->ad_right;
			$bottomAd = $this->_context->ad_bottom;
			$p5 = $this->_context->query('p5', 0);
			$p10 = $this->_context->query('p10', 0);
			$p15 = $this->_context->query('p15', 0);
			$p20 = $this->_context->query('p20', 0);
			$p25 = $this->_context->query('p25', 0);
			$p30 = $this->_context->query('p30', 0);
			$p35 = $this->_context->query('p35', 0);
			$p40 = $this->_context->query('p40', 0);
			$p45 = $this->_context->query('p45', 0);
			$p50 = $this->_context->query('p50', 0);
			$locale = $this->_context->locale;
			if (empty($id))
			{
				$errors[] = Util::getMessage(1000);
			}
			if (empty($domain))
			{
				$errors[] = Util::getMessage(2001);
			}
			if (count($errors) == 0)
			{
				$sitemodel = new Site();
				$sitemodel->update($id, $name, $domain, $game, $jackpot, $jackpotPercent, $template, $monetization1, $monetization2, $monetization3, $monetization4, $topAd, $rightAd
								, $bottomAd, $p5, $p10, $p15, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description, $id);
				$this->_view['id'] = $id;
				$this->_view['name'] = $name;
				$this->_view['domain'] = $domain;
				$this->_view['game'] = $game;
				$this->_view['jackpot'] = $jackpot;
				$this->_view['jackpot_percent'] = $jackpotPercent;
				$this->_view['template'] = $template;
				$this->_view['monetization1'] = $monetization1;
				$this->_view['monetization2'] = $monetization2;
				$this->_view['monetization3'] = $monetization3;
				$this->_view['monetization4'] = $monetization4;
				$this->_view['ad_top'] = $topAd;
				$this->_view['ad_right'] =$rightAd;
				$this->_view['ad_bottom'] = $bottomAd;
				$this->_view['p5'] = $p5;
				$this->_view['p10'] = $p10;
				$this->_view['p15'] = $p15;
				$this->_view['p20'] = $p20;
				$this->_view['p25'] = $p25;
				$this->_view['p30'] = $p30;
				$this->_view['p35'] = $p35;
				$this->_view['p40'] = $p40;
				$this->_view['p45'] = $p45;
				$this->_view['p50'] = $p50;
				$this->_view['locale'] = $locale;
				$this->_view['keywords'] = $keywords;
				$this->_view['description'] = $description;
				$infos[] = Util::getMessage(2003);
			}
			$this->_view['errors'] = $errors;
			$this->_view['infos'] = $infos;
		}
		else if ($this->_context->isGET())
		{
			$id = $this->_context->id;
			$sitemodel = new Site();
			$site = $sitemodel->getSiteById($id);
			$this->_view['id'] = $site['id'];
			$this->_view['name'] = $site['name'];
			$this->_view['domain'] = $site['domain'];
			$this->_view['game'] = $site['game'];
			$this->_view['jackpot'] = $site['jackpot'];
			$this->_view['jackpot_percent'] = $site['jackpot_percent'];
			$this->_view['template'] = $site['template'];
			$this->_view['monetization1'] = $site['monetization1'];
			$this->_view['monetization2'] = $site['monetization2'];
			$this->_view['monetization3'] = $site['monetization3'];
			$this->_view['monetization4'] = $site['monetization4'];
			$this->_view['ad_top'] = $site['ad_top'];
			$this->_view['ad_right'] =$site['ad_right'];
			$this->_view['ad_bottom'] = $site['ad_bottom'];
			$this->_view['p5'] = $site['p5'];
			$this->_view['p10'] = $site['p10'];
			$this->_view['p15'] = $site['p15'];
			$this->_view['p20'] = $site['p20'];
			$this->_view['p25'] = $site['p25'];
			$this->_view['p30'] = $site['p30'];
			$this->_view['p35'] = $site['p35'];
			$this->_view['p40'] = $site['p40'];
			$this->_view['p45'] = $site['p45'];
			$this->_view['p50'] = $site['p50'];
			$this->_view['locale'] = $site['locale'];
			$this->_view['keywords'] = $site['keywords'];
			$this->_view['description'] = $site['description'];
		}
	}
	public function actionDeleteconfirm()
	{
		$id = $this->_context->id;
		if (empty($id))
		{
			return $this->_redirect(url('default/index'));
		}
		$sitemodel = new Site();
		$site = $sitemodel->getSiteById($id);
		if ($site == false)
		{
			return $this->_redirect(url('default/index'));
		}
		$this->_view['id'] = $id;
		$this->_view['domain'] = $site['domain'];
	}
	public function actionDelete()
	{
		$id = $this->_context->id;
		if (empty($id))
		{
			return $this->_redirect(url('default/index'));
		}
		$sitemodel = new Site();
		$sitemodel->deleteSite($id);
		$infos = array();
		$infos[] = Util::getMessage(2004);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('site/show'));
	}
	
	public function actionGetUnsubCsv()
	{
		$parSub_Mdel = new PartnerSubscriber();
		$subUsersId = $parSub_Mdel->getUnSubUsers();
		$csvDatas = array();
		if(!empty($subUsersId)){
			$parSub_Mdel->setUnsub_date($subUsersId);
			
			$userModel = new User();
			$emails = $userModel->getEmailsByIds($subUsersId);
			
			if(!empty($emails)){
				foreach($emails as $email){
					$csvDatas[]['email'] = $email['email'];
				}	
			}
		}
		$csvHeader = array('Email');
		$csvName = "Unsubscribes";
		$this->sendCsv($csvDatas, $csvHeader, $csvName);
	}

	
	private function sendCsv($data, $headers, $filename) {
		
		header('Content-Type: application/csv');
		header('Content-Disposition: attachment; filename="' . $filename . '"');
		$output = fopen('php://output', 'w');
		fputcsv($output, $headers);
		foreach ($data as $row) {
			fputcsv($output, $row);
		}
		
		fclose($output);
		exit;
	}
}
?>